﻿using System.Reflection;
using UnityEngine;
using UnityEditor;

public class UnityEditorTools
{
    [MenuItem("EditorTools/ClearLog(清除Consoles) %&c")]
    public static void ClearConsole()
    {
        var assembly = Assembly.GetAssembly(typeof(ActiveEditorTracker));
        var type = assembly.GetType("UnityEditorInternal.LogEntries");
        if (type == null)
            type = assembly.GetType("UnityEditor.LogEntries");
        var method = type.GetMethod("Clear");
        if (method == null) return; 
        method.Invoke(new object(), null);
    }

    [MenuItem("EditorTools/SetVisible(开关选中GameObjects) %#v")]
    public static void SetSelectedVisible()
    {
        UnityEngine.GameObject[] selected = Selection.gameObjects;
        if (selected.Length <= 0) return;
        bool activite = selected[0].activeInHierarchy;
        for (int i = 0; i < selected.Length; i++)
        {
            selected[i].SetActive(!activite);
        }
    }


    [MenuItem("EditorTools/ApplyPrefab(更新Prefab) %&p")]
    public static void ApplyPrefab()
    {
        GameObject selected = Selection.activeGameObject;
        if (selected == null)
        {
            XLogger.Log("No selected GameObject");
            return;
        }

        UnityEngine.Transform parent = selected.transform;
        GameObject prefab = PrefabUtility.GetPrefabObject(parent) as GameObject;
        if (prefab == null)
        {
            XLogger.Log("Can't find the prefab" + parent.name);
            return;
        }

        PrefabUtility.ReplacePrefab(prefab, selected);
    }

    [MenuItem("GameObject/UI/Rect")]
    public static void CreateRect()
    {
        GameObject selected = Selection.activeGameObject;
        GameObject go = new GameObject("Widget");
        go.AddComponent<RectTransform>();
        if (selected != null) go.SetParent(selected);
        go.transform.localPosition = Vector3.zero;
    }


}